﻿using DevExpress.XtraEditors;
using MDK.Device;
using MDK.Device.Devices;
using MDK.Winform;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform.UserControl.Config
{
    public partial class Cold : BaseForm
    {
        #region 初始化
        public Cold()
        {
            InitializeComponent();
           
        }

        private void Cold_Load(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() => { RefreshDeviceStatus(); });
        }
        #endregion

        #region 刷新设备状态
        public void RefreshDeviceStatus()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(1000);
                    if (this.IsHandleCreated)
                    {
                        //ATM电源
                        BeltS1.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Freezer.BeltCenterSensor.IsActive())
                                BeltS1.Set2();
                            else
                                BeltS1.Set1();
                        }));
                        //ATM电源
                        BeltS2.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Freezer.BeltEndSensor.IsActive())
                                BeltS2.Set2();
                            else
                                BeltS2.Set1();
                        }));

                        //门
                        for (int i = 1; i <= App.flow.SysModel.Freezer.DoorSensors.Count(); i++)
                        {
                            var control = ((Signal)(this.Controls.Find("Door" + i, true)[0]));
                            control.Invoke(new MethodInvoker(() =>
                            {
                                if (App.flow.SysModel.Freezer.DoorSensors[i - 1].IsActive())
                                    control.Set2();
                                else
                                    control.Set1();
                            }));
                        }
                        //挡板
                        for (int i = 1; i <= App.flow.SysModel.Freezer.DamBoardSensors.Count(); i++)
                        {
                            var control = ((Signal)(this.Controls.Find("Dam" + i, true)[0]));
                            control.Invoke(new MethodInvoker(() =>
                            {
                                if (App.flow.SysModel.Freezer.DamBoardSensors[i - 1].IsActive())
                                    control.Set2();
                                else
                                    control.Set1();
                            }));
                        }
                        //计数
                        for (int i = 0; i < App.flow.SysModel.Freezer.CounterSensors.GetLength(0); i++)
                        {
                            for (int j = 0; j < App.flow.SysModel.Freezer.CounterSensors.GetLength(1); j++)
                            {
                                var control = ((Signal)(this.Controls.Find("Count" + i + j, true)[0]));
                                control.Invoke(new MethodInvoker(() =>
                                {
                                    if (App.flow.SysModel.Freezer.CounterSensors[i, j].IsActive())
                                        control.Set2();
                                    else
                                        control.Set1();
                                }));
                            }
                        }
                        //模型
                        circle3.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Freezer.Device.IsAutoMode)
                                circle3.Set2();
                            else if (App.flow.SysModel.Freezer.Device.IsManualMode)
                                circle3.Set1();
                            else
                                circle4.Set3();
                        }));
                        //急停
                        circle4.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Freezer.Device.IsScrammed)
                                circle4.Set2();
                            else
                                circle4.Set1();
                        }));
                        //上限下限温度，当前温度
                        var temperatures = App.flow.SysModel.Freezer.FreezerController.GetTemperatures();
                        txtCurrent1.Invoke(new MethodInvoker(() =>
                        {
                            txtCurrent1.Text = temperatures[0].ToString();
                        }));
                        txtCurrent2.Invoke(new MethodInvoker(() =>
                        {
                            txtCurrent2.Text = temperatures[1].ToString();
                        }));
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }


        #endregion

        #region --详细操作--
        #region 推杆
        private void btnGo_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtP.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                App.flow.SysModel.Freezer.Pushrods[i - 1].StartForward();
                App.flow.SysModel.Freezer.Pushrods[i - 1].WaitCompleted();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtP.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                App.flow.SysModel.Freezer.Pushrods[i - 1].StartRecede();
                App.flow.SysModel.Freezer.Pushrods[i - 1].WaitCompleted();
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtP.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                App.flow.SysModel.Freezer.Pushrods[i - 1].Stop();
                App.flow.SysModel.Freezer.Pushrods[i - 1].WaitCompleted();
            }
        }
        #endregion

        #region 挡板
        private void btnDamGo_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtDam.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                App.flow.SysModel.Freezer.DamBoards[i - 1].StartForward();
                App.flow.SysModel.Freezer.DamBoards[i - 1].WaitCompleted();
            }
        }

        private void btnDamBack_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtDam.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                App.flow.SysModel.Freezer.DamBoards[i - 1].StartRecede();
                App.flow.SysModel.Freezer.DamBoards[i - 1].WaitCompleted();
            }
        }

        private void btnDamStop_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtDam.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                App.flow.SysModel.Freezer.DamBoards[i - 1].Stop();
                App.flow.SysModel.Freezer.DamBoards[i - 1].WaitCompleted();
            }
        }
        #endregion

        #region 皮带
        private void btnBeltGo_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Freezer.Belt.StartForward();
            App.flow.SysModel.Freezer.Belt.WaitCompleted();
        }

        private void btnBeltBack_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Freezer.Belt.StartRecede();
            App.flow.SysModel.Freezer.Belt.WaitCompleted();
        }

        private void btnBeltStop_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Freezer.Belt.Stop();
            App.flow.SysModel.Freezer.Belt.WaitCompleted();
        }
        #endregion

        #region 温度上限
        private void btnUpRead_Click(object sender, EventArgs e)
        {
            txtUp.Text = App.flow.SysModel.Freezer.FreezerController.UpperLimit.ToString();
        }

        private void btnUpWrite_Click(object sender, EventArgs e)
        {
            double i = 0;
            double.TryParse(txtUp.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                App.flow.SysModel.Freezer.FreezerController.UpperLimit = i;
            }
        }
        #endregion

        #region 温度下限

        private void btnDownRead_Click(object sender, EventArgs e)
        {
            txtDown.Text = App.flow.SysModel.Freezer.FreezerController.LowerLimit.ToString();
        }
        private void btnDownWrite_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtDown.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                App.flow.SysModel.Freezer.FreezerController.UpperLimit = i;
            }
        }
        #endregion

        #region 模式
        private void btnAuto_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Freezer.Device.SwitchAutoMode();
        }

        private void btnMu_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Freezer.Device.SwitchManualMode();
        }
        #endregion

        private void btnOpenScrammed_Click(object sender, EventArgs e)
        {

        }

        private void btnCloseScrammed_Click(object sender, EventArgs e)
        {

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Freezer.Device.ClearError();
        }
        #endregion
    }
}
